package com.itheima.game3;

public class test {
    public static void main(String[] args) {
        Student[] student=new Student[3];
        Student st1=new Student(01,"张三",15);
        Student st2=new Student(02,"李四",16);
        Student st3=new Student(03,"王五",17);
        student[0]=st1;
        student[1]=st2;
//        student[2]=st3;
        //要求1-1：添加一个学生对象
        Student st4=new Student(04,"赵六",18);
        //1-2：对id唯一性进行判断，如果不重复就放到数组里
        if(!judExist(student,st4.getId())){
            //判断是否存满
            if(getCount(student)==student.length){
                System.out.println("已满");
                Student[] newArr=creatNewArr(student);
                newArr[newArr.length-1]=st4;
                show(newArr);
            }else{
                student[getCount(student)]=st4;
                show(student);
            }

        }else{
            System.out.println("当前id重复，请重新输入");
        }


    }
    //定义一个方法循环输出
    public static void show(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                System.out.println(arr[i].getId()+","+arr[i].getName()+","+arr[i].getAge());
            }
        }
    }
    //定义一个方法判断id是否重复
    public static boolean judExist(Student[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                if(arr[i].getId()==id){
                    return true;
                }
            }
        }
        return false;
    }
    //定义一个方法判断数组里面已经存了几个
    public static int getCount(Student[] arr){
        int cnt=0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                cnt++;
            }
        }
        return cnt;
    }
    //定义一个方法创建新数组
    public static Student[] creatNewArr(Student[] arr){
        Student[] newArr=new Student[arr.length+1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i]=arr[i];
        }
        return newArr;
    }
}
